<template>
    <div class="app-container dialog-w80">
        <div class="customContainer allocating_container">
            <!-- 搜索头 -->
            <el-form :inline="true" :model="formInline" class="demo-form-inline" size="small">
                <el-form-item>
                    <el-date-picker v-model="pickerDate" value-format="yyyy.MM.dd" type="daterange" align="right"
                        unlink-panels range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
                        :picker-options="pickerOptions"></el-date-picker>
                </el-form-item>
                <el-form-item>
                    <el-select filterable placeholder="选择门店" v-model="formInline.store_id" @change='selectStore'
                        style="width:200px;">
                        <el-option v-for="item in storeList" :key="item.id" :label="item.name" :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-select placeholder="选择仓" v-model="formInline.house_id" style="width:200px;">
                        <el-option v-for="item in storageList" :key="item.house_id" :label="item.house_name"
                            :value="item.house_id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-select placeholder="选择类型" v-model="formInline.species" filterable>
                        <el-option v-for="item in type_list" :key="item.id" :label="item.name" :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-input v-model="formInline.order_no" placeholder="订单查询"></el-input>
                </el-form-item>
                <el-form-item>
                    <dj-button size="small" @search="searchClick" />
                </el-form-item>
            </el-form>
            <!-- 列表 -->
            <el-table highlight-current-row :data="list" v-loading.body="listLoading" border size="small"
                @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="36"></el-table-column>
                <el-table-column align="center" label='序号' width="50">
                    <template slot-scope="scope">
                        <span class="indexColor">{{scope.$index+1+(currentPage-1)*20}}</span>
                    </template>
                </el-table-column>
                <el-table-column align="center" label='订单状态'>
                    <template slot-scope="scope">
                        <span v-if="scope.row.status==0">待审核</span>
                        <span v-if="scope.row.status==1">待出货</span>
                        <span v-if="scope.row.status==2">待收货</span>
                        <span v-if="scope.row.status==3">部分收货</span>
                        <span v-if="scope.row.status==4">订单完结</span>
                        <span v-if="scope.row.status==5">收货完成</span>
                        <span v-if="scope.row.status==6">订单取消</span>
                    </template>
                </el-table-column>
                <el-table-column align="center" label="订单编号">
                    <template slot-scope="scope">
                        <el-button @click="get_order_info(scope.row.id,scope.$index,0)" type="text" size="small">
                            {{scope.row.order_no}}</el-button>
                    </template>
                </el-table-column>
                <el-table-column align="center" label='物品调出仓' prop="house_out_name"></el-table-column>
                <el-table-column align="center" label="物品调入仓" prop="house_in_name">
                </el-table-column>
                <el-table-column align="center" label="下单时间" prop="create_time">
                </el-table-column>
                <el-table-column align="center" label='操作' prop="">
                    <template>
                        开
                    </template>
                </el-table-column>
            </el-table>
            <!-- 导出列表 -->
            <el-table highlight-current-row style="display:none" :data="exportForm" border size="small"
                id="exportContainer">
                <el-table-column align="center" label='序号' width="50">
                    <template slot-scope="scope">
                        <span class="indexColor">{{scope.$index+1+(currentPage-1)*20}}</span>
                    </template>
                </el-table-column>
                <el-table-column align="center" label='订单状态'>
                    <template slot-scope="scope">
                        <span v-if="scope.row.status==0">待审核</span>
                        <span v-if="scope.row.status==1">待出货</span>
                        <span v-if="scope.row.status==2">待收货</span>
                        <span v-if="scope.row.status==3">部分收货</span>
                        <span v-if="scope.row.status==4">订单完结</span>
                        <span v-if="scope.row.status==5">收货完成</span>
                        <span v-if="scope.row.status==6">订单取消</span>
                    </template>
                </el-table-column>
                <el-table-column align="center" label="订单编号">
                    <template slot-scope="scope">
                        <el-button @click="get_order_info(scope.row.id,scope.$index,0)" type="text" size="small">
                            {{scope.row.order_no}}</el-button>
                    </template>
                </el-table-column>
                <el-table-column align="center" label='物品调出仓' prop="house_out_name"></el-table-column>
                <el-table-column align="center" label="物品调入仓" prop="house_in_name">
                </el-table-column>
                <el-table-column align="center" label="下单时间" prop="create_time">
                </el-table-column>
                <el-table-column align="center" label='操作' prop="">
                    <template>
                        开
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <!-- 弹框 -->
        <el-dialog :close-on-click-modal="false" class="dialog_table" title="店内物品调拨出库" :visible.sync="detail_dialog">
            <div class="dialogInContainer" v-loading="dialogLoading">
                <el-table highlight-current-row :data="infolist" v-loading.body="listLoading" border size="small">
                    <el-table-column align="center" label='序号' width="50">
                        <template slot-scope="scope">
                            <span class="indexColor">{{scope.$index+1}}</span>
                        </template>
                    </el-table-column>
                    <el-table-column align="center" label="图片" width="70">
                        <template slot-scope="scope">
                            <img :src="scope.row.thumb" width="50" height="50" alt="">
                        </template>
                    </el-table-column>
                    <el-table-column align="center" label="物品名称" prop="accessories_name"></el-table-column>
                    <el-table-column align="center" label="新物品编号" prop="id"></el-table-column>
                    <el-table-column align="center" label="神采编号" prop="no_obsolete">
                        <template slot-scope="scope">
                            {{scope.row.no_obsolete}}
                        </template>
                    </el-table-column>
                    <el-table-column align="center" label="分类" prop="type_name"></el-table-column>
                    <el-table-column align="center" label="成本价" prop="money_cost"></el-table-column>
                    <el-table-column align="center" label="订单数" prop="order_num"></el-table-column>
                </el-table>
                <!-- 自定义底部 -->
                <div class="bottom_box">
                    <p class="pull-right" style="line-height:0">物品数量合计:{{totalOrder+'  '}}物品金额合计:{{totalPrice|keepTwoNum}}
                    </p>
                </div>
            </div>
        </el-dialog>
        <!-- 自定义底部 -->
        <div class="bottom_box">
            <el-pagination class="pull-left" background @current-change="handleCurrentChange"
                :current-page.sync="currentPage" :page-size="20" layout="prev, pager, next, jumper"
                :total="totalDataNumber">
            </el-pagination>
            <el-button :disabled="exportForm.length==0" class="pull-right" type="primary" size="small"
                @click="exportExcel()">调拨单导出</el-button>
        </div>
    </div>
</template>

<script>
    import {
        getStoreList,
        getStorageList,
        getList,
        getPage,
        getinfoList,
        get_goods_type,
        getExport
    } from "@/api/storage_control/pj_look_in_allocating";
    import { pickerOptionsF, pickerDateF, optionMaxTime } from '@/utils';
    import FileSaver from 'file-saver'
    import XLSX from 'xlsx'
    export default {
        data() {
            return {
                type_list: null,
                dialogLoading: false,
                list: null,
                listLoading: false,
                currentPage: 1,
                totalDataNumber: 1,
                storeList: '',
                storageList: '',
                formInline: {
                    store_id: '',
                    house_id: '',
                    order_no: '',
                    species:'',
                    start_time: "",
                    end_time: "",
                    position: "out",
                    status: 100
                },
                pickerOptions: pickerOptionsF(),
                pickerDate: pickerDateF(),
                detail_dialog: false,
                totalOrder: "0",
                totalPrice: "0",
                infolist: [],
                exportForm: []
            };
        },
        beforeRouteEnter(to, from, next) {
            next((vm) => {//当进入组件之前，执行 beforRouteEnter 路由钩子函数 ,//参数vm就是当前组件的实例。
                if (!vm.type_list) {
                    vm.getGoods();
                    vm.getStore();
                }
            })
        },
        // created() {
        //     this.getStore();
        //     this.getGoods();
        // },
        methods: {
            getGoods() {
                get_goods_type({ category_id: this.$route.meta.id }).then(response => {
                    response.forEach(item => {
                        if (item.name == '配件类物品') {
                            item.id = 1;
                        } else if (item.name == '财务类物品') {
                            item.id = 2;
                        } else if (item.name == '其他类物品') {
                            item.id = 3;
                        }
                    })
                    this.type_list = response;
                    this.formInline.species = response.length > 0 ? response[0].id : '';
                })
            },
            // 选择
            handleSelectionChange(val) {
                this.exportForm = val;
            },
            searchClick() {
                optionMaxTime(this.pickerDate).then(() => {//最大可选择时间区间
                    this.formInline.start_time = this.pickerDate ? this.pickerDate[0] : '';
                    this.formInline.end_time = this.pickerDate ? this.pickerDate[1] : '';
                    this.fetchData();
                });
            },
            // 选择了某个门店
            selectStore() {
                getStorageList({ store_id: this.formInline.store_id, cang: 2 }).then(response => {
                    if(response.length > 0) {
                        this.formInline.house_id = response[0].house_id;
                    }
                    
                    this.storageList = response;
                })
            },
            // 前端导出
            exportExcel() {
                /* generate workbook object from table */
                var wb = XLSX.utils.table_to_book(document.querySelector('#exportContainer'))
                /* get binary string as output */
                var wbout = XLSX.write(wb, { bookType: 'xlsx', bookSST: true, type: 'array' })
                try {
                    FileSaver.saveAs(new Blob([wbout], { type: 'application/octet-stream' }), `${this.$route.meta.title}.xlsx`)
                } catch (e) { if (typeof console !== 'undefined') console.log(e, wbout) }
                return wbout
            },

            // 打开订单详情
            get_order_info(id, index, status) {
                this.dialogLoading = true;
                this.detail_dialog = true;
                getinfoList({ order_id: id, step: 'in' }).then(response => {
                    this.infolist = response;
                    this.count();
                    this.dialogLoading = false;
                });
            },
            // 计算
            count() {
                var totalPrice = 0; //临时总价
                var totalOrder = 0;
                this.infolist.forEach(function (val, index) {
                    totalPrice += val.order_num * 1 * val.money_cost * 1; //累计总价
                    totalOrder += val.order_num * 1;
                });
                this.totalPrice = parseFloat(totalPrice);
                this.totalOrder = parseFloat(totalOrder);
            },
            // 获取门店列表
            getStore() {
                getStoreList({ category_id: 336 }).then(response => {
                    this.storeList = response;
                   let data = response.filter(res => 
                        res.id == this.$store.getters.optionId
                    )
                    if(data.length>0){
                        this.formInline.store_id = data[0].id;
                    } else { 
                        if(this.storeList.length > 0) {
                            this.formInline.store_id = this.storeList[0].id;
                        }
                    }
                    this.selectStore();
                })
            },
            // 列表获取
            fetchData() {
                this.listLoading = true;
                this.currentPage = 1;
                getPage(this.formInline).then(response => {
                    this.totalDataNumber = response * 20;
                    getList(this.formInline).then(response => {
                        this.list = response;
                        this.listLoading = false;
                    });
                }).catch(res=>{
                    this.listLoading=false;
                });
            },
            // 分页
            handleCurrentChange(val) {
                this.listLoading = true;
                getList(this.formInline, val).then(response => {
                    this.list = response;
                    this.listLoading = false;
                });
            },
        }
    };
</script>